#include "passwordmodify.h"
#include "ui_passwordmodify.h"
#include "commonfunction.h"
#include "sqlhelper.h"
#include "blldataflow.h"
#include "project.h"
#include "shadow.h"
#include "frminput2019.h"
#include "msgbox.h"
#include "userlogger.h"

PasswordModify::PasswordModify(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PasswordModify)
{
    ui->setupUi(this);

    bdf = BllDataFlow::getInstance();
    connect(bdf, &BllDataFlow::buildFinishedSignal, this, &PasswordModify::setWidgetParent);
    connect(ui->linePassword, &QLineEdit::textChanged, this, &PasswordModify::onClearError);
    connect(ui->lineConfirmPassword, &QLineEdit::textChanged, this, &PasswordModify::onClearError);
    connect(ui->btnConfirm, &QToolButton::clicked, this, &PasswordModify::onConfirm);
    connect(ui->btnCancel, &QToolButton::clicked, this, &PasswordModify::onCancel);
    connect(ui->btnShowPw, &QToolButton::toggled, this, &PasswordModify::onBtnShowPwToggled);
    connect(ui->btnShowConfirmPw, &QToolButton::toggled, this, &PasswordModify::onBtnShowConfirmPwToggled);

    setShadow({ui->widget});
    if (windowWidth >= 1280)
    {
        this->setProperty("cssSize", "large");
        this->setGeometry(0,0,1280,800);
        ui->btnShowPw->setMaximumSize(70, 70);
        ui->btnShowConfirmPw->setMaximumSize(70, 70);
    }

    ui->linePassword->setValidator(gPwValidator);
    ui->lineConfirmPassword->setValidator(gPwValidator);

    onClearError();
}

PasswordModify::~PasswordModify()
{
    delete ui;
}

void PasswordModify::setWidgetParent()
{
    this->setParent(Project::Instance());
}

void PasswordModify::onBtnShowPwToggled(bool checked)
{
    if(checked)
    {
        QString style = "border:none;border-image:url(:/resources/eye_open.png)";
        ui->btnShowPw->setStyleSheet(style);
        ui->linePassword->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        QString style = "border:none;border-image:url(:/resources/eye_close.png)";
        ui->btnShowPw->setStyleSheet(style);
        ui->linePassword->setEchoMode(QLineEdit::Password);
    }
}

void PasswordModify::onBtnShowConfirmPwToggled(bool checked)
{
    if(checked)
    {
        QString style = "border:none;border-image:url(:/resources/eye_open.png)";
        ui->btnShowConfirmPw->setStyleSheet(style);
        ui->lineConfirmPassword->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        QString style = "border:none;border-image:url(:/resources/eye_close.png)";
        ui->btnShowConfirmPw->setStyleSheet(style);
        ui->lineConfirmPassword->setEchoMode(QLineEdit::Password);
    }
}
void PasswordModify::onClearError()
{
    ui->lblConfirmError->clear();
    ui->lblPasswordError->clear();
    ui->label_6->setVisible(false);
    ui->label_7->setVisible(false);
}

void PasswordModify::onConfirm()
{
    ui->linePassword->deselect();
    ui->lineConfirmPassword->deselect();
    frmInput2019::Instance()->hideAndClearFocus();

    QString password = ui->linePassword->text().trimmed();
    QString passwordAgain = ui->lineConfirmPassword->text().trimmed();

    if (!checkPwValid(password))
    {
        ui->linePassword->selectAll();
        ui->lblPasswordError->setText(tr("Passwords does not meet requirements"));
        return;
    }

    if (password != passwordAgain)
    {
        ui->lineConfirmPassword->selectAll();
        ui->lblConfirmError->setText(tr("The passwords entered twice do not match"));
        return;
    }

    onClearError();

    EttaError ret;
    QString message;
    SqlHelper *sqlHelper = SqlHelper::getInstance();

    User user;
    user.name = currentUsername;
    user.password = password;
    ret = sqlHelper->setUserPassword(user);

    if (OK == ret)
    {
        closeForm();
        message = QString("%1, %2").arg(tr("Succeed to modify password")).arg(tr("please login again."));
        MsgBox::information(this, tr("Tips"), message);
        emit done();
    }
    else
    {
        message = tr("Failed to modify password");
        MsgBox::information(this, tr("Tips"), message);
    }

    UserLogger::log(message);
}

void PasswordModify::onCancel()
{
    UserLogger::log(tr("Cancel to modify password"));
    closeForm();
}

void PasswordModify::closeForm()
{
    close();
    ui->linePassword->clear();
    ui->lineConfirmPassword->clear();
    onBtnShowConfirmPwToggled(false);
    onBtnShowPwToggled(false);
}

